package com.hemaapp.yjnh.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hemaapp.yjnh.bean.Address;
import com.hemaapp.hm_FrameWork.HemaAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.AddressSelectActivity;

public class AddressAadapter extends HemaAdapter {

	private Context mContext;
	private ArrayList<Address> addresss;

	public AddressAadapter(Context mContext, ArrayList<Address> addresss) {
		super(mContext);
		this.mContext = mContext;
		this.addresss = addresss;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return addresss.size() == 0 ? 1 : addresss.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (addresss.size() == 0) {
			return getEmptyView(parent);
		}
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.listitem_district, null);
			ViewHolder holder = new ViewHolder();
			findHolder(convertView, holder);
			convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
		}
		Address address = addresss.get(position);
		ViewHolder holder = (ViewHolder) convertView
				.getTag(R.id.TAG_VIEWHOLDER);
		holder.name.setText(address.getName());
		convertView.setTag(address);
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Address address=(Address) v.getTag();
				Intent it=((AddressSelectActivity)mContext).getIntent();
				it.putExtra("address",address );
				((AddressSelectActivity)mContext).setResult(-1, it);
				((AddressSelectActivity)mContext).finish();
			}
		});
		return convertView;
	}

	private void findHolder(View convertView, ViewHolder holder) {
		holder.name = (TextView) convertView.findViewById(R.id.name);
	}

	private class ViewHolder {
		private TextView name;
	}
}
